import 'package:Lter/config/app.dart';
import 'package:flutter/material.dart';
import 'package:Lter/model/user.dart';
import 'package:Lter/model/lter.dart';
import 'package:Lter/model/phone.dart';
import 'package:Lter/util/websocket.dart';

class AppInherited {
  WebSocket mWebSocket = WebSocket(APP_CONFIG.WEBSOCKET_URL);
  User mUser = User();
  List<PhoneInfo> mPhoneInfoList = List<PhoneInfo>();
  LterServerInfo mLterServerInfo = LterServerInfo();
  List<LterClientInfo> mLterClientInfoList = List<LterClientInfo>();
}

/**
 * 利用InheritedWidget用于子节点向祖先节点获取数据
    当依赖的InheritedWidget rebuild,会触发子控件的didChangeDependencies()接口
 */
class AppProvider extends InheritedWidget {
  final Widget child;
  final AppInherited app; //在子树中共享的数据

  AppProvider({this.app, this.child}) : super(child: child);

  /**
   * 该回调决定当数据发生变化时，是否通知子树中依赖数据的Widget
   */
  @override
  bool updateShouldNotify(InheritedWidget oldWidget) {
    return true;
  }
}

/**
 * 需要一个StatefulWidget作为外层的组件，
    将我们的继承于InheritateWidget的组件build出去
 */
class AppContainer extends StatefulWidget {
  //给子控件分享的数据
  final AppInherited app;
  final Widget child;

  AppContainer({Key key, this.app, this.child}) : super(key: key);

  @override
  _UserContainerState createState() => _UserContainerState();

  static AppProvider of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(AppProvider);
  }
}

class _UserContainerState extends State<AppContainer> {
  @override
  Widget build(BuildContext context) {
    return AppProvider(app: widget.app, child: widget.child);
  }
}
